package com.ming.yourdiary.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Outline;
import android.location.Location;
import android.location.LocationManager;

import androidx.core.app.ActivityCompat;
import androidx.core.view.ViewCompat;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.Window;
import android.view.WindowManager;

import com.ming.yourdiary.YourDiaryApplication;

public class UiTools {

  private static Context sContext = YourDiaryApplication.sContext;

  //设置状态栏背景颜色
  public static void setStatusBarColor(Activity activity, int statusColor) {
    Window window = activity.getWindow();
    //取消状态栏透明
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //添加Flag把状态栏设为可绘制模式
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    //设置状态栏颜色
    window.setStatusBarColor(statusColor);
    //设置系统状态栏处于可见状态
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    //让view不根据系统窗口来调整自己的布局
    ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
      ViewCompat.setFitsSystemWindows(mChildView, false);
      ViewCompat.requestApplyInsets(mChildView);
    }
  }

  //设置透明状态栏
  public static void setTranslucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
    Window window = activity.getWindow();
    //添加Flag把状态栏设为可绘制模式
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    if (hideStatusBarBackground) {
      //如果为全透明模式，取消设置Window半透明的Flag
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      //设置状态栏为透明
      window.setStatusBarColor(Color.TRANSPARENT);
      //设置window的状态栏不可见
      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    } else {
      //如果为半透明模式，添加设置Window半透明的Flag
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      //设置系统状态栏处于可见状态
      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
    }
    //view不根据系统窗口来调整自己的布局
    ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
      ViewCompat.setFitsSystemWindows(mChildView, false);
      ViewCompat.requestApplyInsets(mChildView);
    }
  }

  //设置状态栏文本颜色
  public static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
    View decor = activity.getWindow().getDecorView();
    if (dark) {
      decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    } else {
      decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }
  }


  //View动态设置圆角
  public static void setBgRadius(View layoutContent, int radius) {
    layoutContent.setOutlineProvider(new ViewOutlineProvider() {
      @Override
      public void getOutline(View view, Outline outline) {
        outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius);
      }
    });
    layoutContent.setClipToOutline(true);
  }


  //获取Dimens
  public static int getDimensionPixelOffset(int id) {
    if (sContext != null){
      return sContext.getResources().getDimensionPixelOffset(id);
    }
    return 0;
  }


  //使用GPS获取用户位置
  public static String getUserLocationByGPS() {
    // 获取的是位置服务
    String serviceString = Context.LOCATION_SERVICE;
    // 调用getSystemService()方法来获取LocationManager对象
    LocationManager locationManager = (LocationManager) sContext.getSystemService(serviceString);
    String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法
    if (ActivityCompat.checkSelfPermission(YourDiaryApplication.sContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
      && ActivityCompat.checkSelfPermission(sContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      // 调用getLastKnownLocation()方法获取当前的位置信息
      Location location = locationManager.getLastKnownLocation(provider);
      double lat = location.getLatitude();//获取纬度
      double lng = location.getLongitude();//获取经度
      return lat + "," + lng;
    }
    return null;
  }


}
